package com.poker.wiki.controller;



import com.github.pagehelper.PageInfo;
import com.poker.common.utils.RedisUtil;
import com.poker.common.utils.RequestContext;
import com.poker.common.utils.Result;

import org.springframework.web.bind.annotation.*;
import com.poker.wiki.Service.ContentService;
import com.poker.wiki.Service.DocService;
import com.poker.wiki.entity.Doc;
import com.poker.wiki.req.DocQueryReq;
import com.poker.wiki.req.DocSaveReq;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;


@RestController
public class DocController {
    @Resource
    DocService docService;

    @Resource
    ContentService contentService;

    @RequestMapping("getAllDoc")
    public Result getAllDoc() {
        List<Doc> allDoc = docService.getAllDoc();
        return Result.success(allDoc);
    }

    @RequestMapping("getDocByEbookId")
    public Result getDocByEbookId(@RequestParam(name = "id") Long id) {
        List<Doc> docs = docService.getDocByEbookId(id);
        return Result.success(docs);
    }

    @RequestMapping("findDoc")
    public Result findDoc(@Valid DocQueryReq req) {
        PageInfo<Doc> pageInfo = docService.findDoc(req);
        return Result.success(pageInfo);
    }

    @DeleteMapping("/deleteDoc/{id}")
    public Result delete(@PathVariable Long id){
        docService.delete(id);
        return Result.success();
    }

    @PostMapping("/saveDoc")
    public Result save(@RequestBody @Valid DocSaveReq req){
        docService.save(req);
        return Result.success();
    }

    @RequestMapping("/findContent/{id}")
    public Result findContent(@PathVariable Long id) {
        docService.updateDocView(id);
        String content = contentService.findContent(id).getContent();
        return Result.success(content);
    }

    @GetMapping("/docVote/{id}")
    public Result docVote(@PathVariable Long id, HttpServletRequest request) {

        RequestContext.setRemoteAddr(request.getHeader("x-forwarded-for"));

        docService.docVote(id);
        return Result.success();
    }

    @GetMapping("/docVoteStatus/{id}")
    public Result docVoteStatus(@PathVariable Long id) {
        boolean boo = docService.docVoteStatus(id);
        return Result.success(boo);
    }

}
